package com.ruan.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.entity.Address;
import com.ruan.entity.R;
import com.ruan.mapper.AddressMapper;
import com.ruan.requestdata.AddressRequestData;
import com.ruan.service.AddressService;
import com.ruan.utils.RedisCache;
import com.ruan.vo.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 59499
 * @description 针对表【address】的数据库操作Service实现
 * @createDate 2023-09-13 20:13:30
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address>
        implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public R<Void> addAddress(AddressRequestData addressRequestData) {
        Address address = new Address();
        address.setProvince(addressRequestData.getRegion()[0]);
        address.setCity(addressRequestData.getRegion()[1]);
        address.setDistrict(addressRequestData.getRegion()[2]);
        address.setConsignee(addressRequestData.getConsignee());
        address.setDetailAddress(addressRequestData.getAddress());
        address.setPhoneNumber(addressRequestData.getPhoneNumber());
        int row = addressMapper.insert(address);
        if (row != 1) {
            return new R<>(100, "添加失败");
        }
        int row1 = addressMapper.insertUserAddress(addressRequestData.getUserId(), address.getId());
        if (row1 != 1) {
            return new R<>(100, "添加失败");
        }
        redisCache.deleteObject("getAddressByUserId" + addressRequestData.getUserId());
        return new R<>(200, "添加成功");
    }

    @Override
    public R<List<AddressVo>> getAddressByUserId(String userId) {
        List<AddressVo> addressVoList = redisCache.getCacheList("getAddressByUserId" + userId);
        if (!addressVoList.isEmpty()) {
            return new R<>(200, "查询成功", addressVoList);
        }
        List<Integer> addressIdList = addressMapper.getAddressIdsByUserId(userId);
        List<AddressVo> addressVoList1 = new ArrayList<>();
        for (Integer id : addressIdList) {
            QueryWrapper<Address> wrapper = new QueryWrapper<>();
            wrapper.eq("id", id);
            wrapper.eq("status", 1);
            Address address = addressMapper.selectOne(wrapper);
            if (address != null) {
                AddressVo addressVo = new AddressVo();
                addressVo.setId(address.getId());
                addressVo.setAddress("收件人：" + address.getConsignee() + " 手机号：" + address.getPhoneNumber() + " 地址：" + address.getProvince() + address.getCity() + address.getDistrict() + address.getDetailAddress());
                addressVoList1.add(addressVo);
            }
        }
        if (!addressVoList1.isEmpty()) {
            redisCache.setCacheList("getAddressByUserId" + userId, addressVoList1);
        }
        return new R<>(200, "查询成功", addressVoList1);
    }

    @Override
    public R<Address> getAddressById(Integer id) {
        Address address = addressMapper.selectById(id);
        return new R<>(200, "查询成功", address);
    }

    @Override
    public R<Void> editAddress(AddressRequestData addressRequestData) {
        Address address = new Address();
        address.setId(addressRequestData.getId());
        address.setProvince(addressRequestData.getRegion()[0]);
        address.setCity(addressRequestData.getRegion()[1]);
        address.setDistrict(addressRequestData.getRegion()[2]);
        address.setConsignee(addressRequestData.getConsignee());
        address.setDetailAddress(addressRequestData.getDetailAddress());
        address.setPhoneNumber(addressRequestData.getPhoneNumber());
        int row = addressMapper.updateById(address);
        if (row != 1) {
            return new R<>(100, "修改失败");
        }
        redisCache.deleteObject("getAddressByUserId" + addressRequestData.getUserId());
        return new R<>(200, "修改成功");
    }

    @Override
    public R<Void> deleteAddress(Integer id, String userId) {
        Address address = addressMapper.selectById(id);
        address.setStatus(0);
        int row = addressMapper.updateById(address);
        if (row != 1) {
            return new R<>(100, "删除失败");
        }
        redisCache.deleteObject("getAddressByUserId" + userId);
        return new R<>(200, "修改成功");
    }
}




